package com.hzb.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.hzb.gulimall.product.entity.ProductAttrValueEntity;
import com.hzb.gulimall.product.service.ProductAttrValueService;
import com.hzb.gulimall.product.vo.AttrRespVo;
import com.hzb.gulimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hzb.gulimall.product.service.AttrService;
import com.hzb.common.utils.PageUtils;
import com.hzb.common.utils.R;



/**
 * 
 *
 * @author huangZB
 * @email 576566606@qq.com
 * @date 2020-08-26 23:22:10
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Autowired
    private ProductAttrValueService productAttrValueService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * /product/attr/base/listforspu/{spuId}
     */
    @RequestMapping("/base/listforspu/{spuId}")
    public R baseListForSpu(@PathVariable("spuId") Long spuId){
        List<ProductAttrValueEntity> productAttrValueEntities =  productAttrValueService.baseListForSpu(spuId);
        return R.ok().put("data",productAttrValueEntities);
    }

    @RequestMapping("/{attrType}/list/{catelogId}")
    //@RequiresPermissions("product:attr:list")
    public R queryBasePage(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Long catelogId,
                           @PathVariable("attrType") String attrType){
        PageUtils page = attrService.queryBasePage(params,catelogId,attrType);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){

        AttrRespVo attrRespVo = attrService.getAttrVo(attrId);
        return R.ok().put("attr", attrRespVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attr){
		attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVo attr){
		//attrService.updateById(attr);

        attrService.updateAttr(attr);
        return R.ok();
    }
    /**
     * /product/attr/update/{spuId}
     */
    @RequestMapping("/update/{spuId}")
    //@RequiresPermissions("product:attr:update")
    public R updateSpuInfo(@PathVariable("spuId") Long spuId,
                           @RequestBody List<ProductAttrValueEntity> productAttrValueEntities){

        productAttrValueService.updateSpuInfo(spuId,productAttrValueEntities);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }




}
